package com.myz.rabbitmq.study7;

import com.myz.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogsTopic02 {
    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception {
        // 1.获取信道
        Channel channel = RabbitMqUtils.getChannel();
        // 2.声明交换机 declare exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 3.声明队列 declare queue
        String queueName = "Q2";
        channel.queueDeclare(queueName,false,false,false,null); // queueName,持久化,唯一性,自动删除,其它
        DeliverCallback deliverCallback = (consumeTag,message) -> {
            System.out.println("C2 receive a message:"+new String(message.getBody(),"UTF-8"));
//            System.out.println("receive queue:"+queueName+" routingKey:"+message.getEnvelope().getRoutingKey());
        };
        // 4.绑定队列
        channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
        System.out.println("ReceiveLogsTopic02 waiting receive message...");
        System.out.println("*.*.rabbit   lazy.#");
        // 5.接收消息
        channel.basicConsume(queueName,true,deliverCallback, (CancelCallback) null);
    }
}
